<template>
  <a-modal
    :title="title"
    :width="500"
    v-bind="modalOption"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-form-model-item label="表名" prop="tableName">
          <a-input placeholder="请输入表名" :disabled="disableSubmit" v-model.trim="form.tableName"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import ModalFormMixin from '@/mixins/ModalFormMixin'
import { putAction, postAction } from '@/services/manage'
export default {
  name: 'DbTableModal',
  mixins: [ModalFormMixin],
  data() {
    return {
      title: '操作',
      validatorRules: {
        tableName: [
          { required: true, message: '请输入字典编码!', trigger: 'change' }
        ]
      },
      url: {
        add: '/dbconsole/table/create/',
        edit: '/dbconsole/table/alert/'
      },
      dbData: {}
    }
  },
  methods: {
    addAfter(data) {
      this.dbData = data
    },
    editAfter(data) {
      this.dbData = data
    },
    handleOk() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.editType === 'edit') {
            putAction(this.url.edit + this.dbData.dbInfoId + '/' + this.model.tableName + '/' + this.form.tableName).then(res => {
              if (res.success) {
                this.$emit('ok')
                this.close()
                this.$message.success('操作成功')
              }
            })
          } else {
            postAction(this.url.add + this.dbData.id + '/' + this.form.tableName).then(res => {
              if (res.success) {
                this.$emit('ok')
                this.close()
                this.$message.success('操作成功')
              }
            })
          }
        }
      })
    }
  }
}
</script>
